import { GET, POST } from "./config"
import axios from "axios"

//创建配置axios实例
const instance = axios.create({
    baseURL: "http://api.kudesoft.cn/tdk",//发送请求时会在url前拼接baseURL
    timeout: 10000,
    //设置axios为form-data
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },

    transformRequest: [function (data) {
        let ret = ''
        for (let it in data) {
            ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        }
        return ret
    }]
})

export function request(method, url, params) {
    switch (method) {
        case GET:
            //调用函数对象，返回它本身
            return get(url, params)
        case POST:
            return post(url, params)
    }
}

//核心请求方法
function get(url, params) {
    return instance.get(url, params)
}
function post(url, params) {
    return instance.post(url, params)
}